package com.aspose.cells.a.f;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

class zf extends za {
   private byte[] c;
   private byte[][] d;
   private int[] e;
   private zf.zc f;

   zf(InputStream var1) throws Exception {
      this.c = a(var1);
      this.e();
   }

   private void e() throws IOException {
      String[] var1 = new String[50];
      int[] var2 = new int[50];
      int[] var3 = new int[50];
      byte[][] var4 = new byte[20][];
      int[] var5 = new int[20];
      int[] var6 = new int[20];
      int var7 = -1;
      int var8 = -1;
      int var9 = 0;
      int var10 = -1;
      this.f = new zf.zc(this.c);
      BufferedInputStream var11 = new BufferedInputStream(this.f);
      byte[] var12 = new byte[1048576];

      while(true) {
         if (this.f.e) {
            var10 = this.f.a();
         }

         ZipEntry var13 = this.f.getNextEntry();
         if (var13 == null) {
            ++var7;
            if (var7 == var1.length) {
               this.a = var1;
               this.b = var2;
               if (this.f.e) {
                  this.e = var3;
               }
            } else {
               this.a = new String[var7];
               System.arraycopy(var1, 0, this.a, 0, var7);
               this.b = new int[var7];
               System.arraycopy(var2, 0, this.b, 0, var7);
               if (this.f.e) {
                  this.e = new int[var7];
                  System.arraycopy(var3, 0, this.e, 0, var7);
               }
            }

            if (!this.f.e) {
               this.f = null;
               ++var8;
               if (var8 == var4.length) {
                  this.d = var4;
               } else {
                  this.d = new byte[var8][];
                  System.arraycopy(var4, 0, this.d, 0, var8);
               }
            }

            return;
         }

         if (!var13.isDirectory()) {
            ++var7;
            if (var7 == var1.length) {
               String[] var14 = new String[var7 << 1];
               int[] var15 = new int[var14.length];
               System.arraycopy(var1, 0, var14, 0, var7);
               System.arraycopy(var2, 0, var15, 0, var7);
               var1 = var14;
               var2 = var15;
               if (this.f.e) {
                  var15 = new int[var14.length];
                  System.arraycopy(var3, 0, var15, 0, var7);
                  var3 = var15;
               }
            }

            var1[var7] = var13.getName().replace('\\', '/');
            int var19;
            if (this.f.e) {
               var3[var7] = var10;

               for(var19 = 0; var11.read() > -1; var19 = (int)((long)var19 + var11.skip(1048576L))) {
                  ++var19;
               }

               var2[var7] = var19;
            } else {
               var19 = 0;

               int var20;
               for(var20 = 0; var19 < 1048576; var19 += var20) {
                  var20 = var11.read(var12, var19, 1048576 - var19);
                  if (var20 < 1) {
                     break;
                  }
               }

               if (var20 > -1) {
                  while(var11.read() > -1) {
                     ++var19;
                     var19 = (int)((long)var19 + var11.skip(1048576L));
                  }
               }

               if (var19 > 1048576) {
                  var2[var7] = var19;
               } else if (var19 > 0) {
                  if (var9 + var19 >= 16777216) {
                     int var16 = var5[var8];
                     if (var19 < var4[var16].length) {
                        var2[var6[var16]] = var4[var16].length;
                        var9 -= var4[var16].length;
                        var9 += var19;
                        byte[] var17 = new byte[var19];
                        System.arraycopy(var12, 0, var17, 0, var19);
                        var4[var16] = var17;
                        var6[var16] = var7;
                        var2[var7] = -var16 - 1;
                        int var18 = this.a(var19, var4, var5, var8 - 1);
                        System.arraycopy(var5, var18, var5, var18 + 1, var8 - var18);
                        var5[var18] = var8;
                     } else {
                        var2[var7] = var19;
                     }
                  } else {
                     ++var8;
                     if (var8 == var4.length) {
                        byte[][] var21 = new byte[var8 << 1][];
                        System.arraycopy(var4, 0, var21, 0, var8);
                        var4 = var21;
                        int[] var23 = new int[var21.length];
                        System.arraycopy(var6, 0, var23, 0, var8);
                        var6 = var23;
                        var23 = new int[var23.length];
                        System.arraycopy(var5, 0, var23, 0, var8);
                        var5 = var23;
                     }

                     var9 += var19;
                     byte[] var22 = new byte[var19];
                     System.arraycopy(var12, 0, var22, 0, var19);
                     var4[var8] = var22;
                     var6[var8] = var7;
                     var2[var7] = -var8 - 1;
                     int var24 = this.a(var19, var4, var5, var8 - 1);
                     System.arraycopy(var5, var24, var5, var24 + 1, var8 - var24);
                     var5[var24] = var8;
                  }
               } else {
                  var2[var7] = var19;
               }
            }
         }

         this.f.closeEntry();
      }
   }

   private int a(int var1, byte[][] var2, int[] var3, int var4) {
      while(var4 > -1 && var2[var3[var4]].length > var1) {
         --var4;
      }

      return var4 + 1;
   }

   public int b(int var1) {
      int var2 = this.b[var1];
      return var2 < 0 ? this.d[-var2 - 1].length : var2;
   }

   public InputStream c(String var1) throws Exception {
      int var2 = this.b(var1);
      if (var2 < 0) {
         return null;
      } else if (this.b[var2] < 0) {
         return new ByteArrayInputStream(this.d[-this.b[var2] - 1]);
      } else if (this.b[var2] == 0) {
         return new ByteArrayInputStream(new byte[0]);
      } else {
         if (this.f == null) {
            this.f = new zf.zc(this.c);
         } else {
            if (this.f.e) {
               if (!this.f.d()) {
                  this.f = new zf.zc(this.c);
               }

               this.f.a(this.e[var2]);
               this.f.getNextEntry();
               this.f.c();
               return this.f;
            }

            if (!this.f.b(var2)) {
               this.f = new zf.zc(this.c);
            }
         }

         while(true) {
            ZipEntry var3 = this.f.getNextEntry();
            if (var3 == null) {
               this.f = null;
               return null;
            }

            if (!var3.isDirectory() && this.f.b() == var2) {
               this.f.c();
               return this.f;
            }

            this.f.closeEntry();
         }
      }
   }

   public void c() throws Exception {
   }

   public boolean d() throws Exception {
      return true;
   }

   static byte[] a(InputStream var0) throws IOException {
      ByteArrayOutputStream var1 = new ByteArrayOutputStream();
      BufferedInputStream var2 = new BufferedInputStream(var0);
      byte[] var3 = new byte[1024];

      int var4;
      while((var4 = var2.read(var3)) > 0) {
         var1.write(var3, 0, var4);
      }

      return var1.toByteArray();
   }

   class zc extends ZipInputStream {
      private int b = -1;
      private boolean c = false;
      private boolean d = false;
      private final boolean e;

      zc(byte[] var2) {
         super(zf.this.new za(var2));
         if (com.aspose.cells.a.za.a && this.in instanceof PushbackInputStream) {
            this.in = zf.this.new zb(var2, 512);
            this.e = true;
         } else {
            this.e = false;
         }

      }

      public int a() {
         return ((zf.zb)this.in).b();
      }

      public void a(int var1) {
         ((zf.zb)this.in).c();
         ((zf.za)((zf.zb)this.in).a()).a(var1);
      }

      public int b() {
         return this.b;
      }

      public void c() {
         this.c = true;
      }

      public boolean d() throws IOException {
         if (this.d) {
            return true;
         } else if (this.c) {
            if (this.available() < 1) {
               this.closeEntry();
               return true;
            } else {
               return false;
            }
         } else {
            this.closeEntry();
            return true;
         }
      }

      public boolean b(int var1) throws IOException {
         return this.b >= var1 ? false : this.d();
      }

      public ZipEntry getNextEntry() throws IOException {
         ZipEntry var1 = super.getNextEntry();
         if (var1 == null) {
            return null;
         } else {
            if (!var1.isDirectory()) {
               ++this.b;
            }

            this.d = false;
            return var1;
         }
      }

      public void close() throws IOException {
         this.closeEntry();
      }

      public void closeEntry() throws IOException {
         super.closeEntry();
         this.d = true;
         this.c = false;
      }
   }

   class zb extends PushbackInputStream {
      zb(byte[] var2, int var3) {
         super(zf.this.new za(var2), var3);
      }

      public InputStream a() {
         return this.in;
      }

      public int b() {
         return ((zf.za)this.in).a() - (this.buf.length - this.pos);
      }

      public void c() {
         this.pos = this.buf.length;
      }
   }

   class za extends ByteArrayInputStream {
      public za(byte[] var2) {
         super(var2);
      }

      public int a() {
         return this.pos;
      }

      public void a(int var1) {
         this.pos = var1;
      }
   }
}
